package com.xiaoshuidi.cloud.module.system.controller.admin.oauser;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy;
import com.xiaoshuidi.cloud.framework.common.pojo.CommonResult;
import com.xiaoshuidi.cloud.framework.common.pojo.PageResult;
import com.xiaoshuidi.cloud.module.system.controller.admin.oauser.vo.OaUserRespVO;
import com.xiaoshuidi.cloud.module.system.controller.admin.oauser.vo.OaUserSearchVO;
import com.xiaoshuidi.cloud.module.system.controller.admin.oauser.vo.UserImportExcelVO;
import com.xiaoshuidi.cloud.module.system.controller.admin.oauser.vo.UserImportRespVO;
import com.xiaoshuidi.cloud.module.system.convert.oauser.OaUserConvert;
import com.xiaoshuidi.cloud.module.system.dal.dataobject.oauser.OaUsersDO;
import com.xiaoshuidi.cloud.module.system.service.oauser.OaUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.annotation.security.PermitAll;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static com.xiaoshuidi.cloud.framework.common.pojo.CommonResult.success;

@Tag(name =  "管理后台 - oa")
@RestController
@RequestMapping("/system/oa/user")
@Validated
public class OaController {
		@Resource
		private OaUserService oaUserService;

		@GetMapping("/page")
		@PermitAll
		@Operation(summary = "获取用户信息")
		public CommonResult<PageResult<OaUserRespVO>> getPage(@Valid OaUserSearchVO searchVO) {
				PageResult<OaUsersDO> pageResult = oaUserService.getPage(searchVO);
				if (CollectionUtil.isEmpty(pageResult.getList())) {
						return success(new PageResult<>(pageResult.getTotal()));
				}
				List<OaUserRespVO> pageList = new ArrayList<>(pageResult.getList().size());
				pageResult.getList().forEach(c -> {
						OaUserRespVO oaUserRespVO = OaUserConvert.INSTANCE.convert(c);
						pageList.add(oaUserRespVO);
				});
				return success(new PageResult<>(pageList, pageResult.getTotal()));
		}



		@GetMapping("/get-import-template")
		@PermitAll
		@Operation(summary = "获得导入用户模板")
		public void importTemplate(HttpServletResponse response) throws IOException {
				// 手动创建导出 demo
				List<UserImportExcelVO> list = Arrays.asList(
												UserImportExcelVO.builder().username("zhangsan").mobile("19911111111").build(),
												UserImportExcelVO.builder().username("lisi").mobile("19911111112").build()
				);

				// 输出 Excel
				ExcelWriter w = EasyExcel.write(response.getOutputStream())
												.autoCloseStream(false) // 不要自动关闭，交给 Servlet 自己处理
												.registerWriteHandler(new LongestMatchColumnWidthStyleStrategy()).build(); // 基于 column 长度，自动适配。最大 255 宽度
				WriteSheet sheet = EasyExcel.writerSheet("用户列表").head(UserImportExcelVO.class).build();
				w.write(list, sheet);

				// 设置 header 和 contentType。写在最后的原因是，避免报错时，响应 contentType 已经被修改了
				response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("用户导入模板.xls", "UTF-8"));
				response.setContentType("application/vnd.ms-excel;charset=UTF-8");
				w.finish();
		}


		@PostMapping("/import")
		@PermitAll
		@Operation(summary = "导入用户")
		public CommonResult<UserImportRespVO> importExcel(@RequestParam("file") MultipartFile file) throws Exception {
				List<UserImportExcelVO> list =
												EasyExcel.read(file.getInputStream(), UserImportExcelVO.class, null)
																				.autoCloseStream(false)
																				.sheet(0).doReadSync();
				return success(oaUserService.importUserList(list));
		}

}
